package cz.khfotbal.dao.impl;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import cz.khfotbal.dao.SeasonDao;
import cz.khfotbal.domain.Competion;
import cz.khfotbal.domain.Season;

@Repository("seasonDaoImpl")
@Transactional
public class SeasonDaoImpl implements SeasonDao {

	@Autowired
	private SessionFactory sessionFactory;

	private Session session;

	public void saveSeason(Season season) {
		session = sessionFactory.getCurrentSession();
		session.saveOrUpdate(season);
	}

	public List<Season> findAllSeasons() {
		session = sessionFactory.getCurrentSession();
		List<Season> list = session.createQuery("from Season").list();
		return list;
	}

	public void updateSeason(Season season) {
		session = sessionFactory.getCurrentSession();
		session.merge(season);
	}

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public Season findSeasonByName(String name) {
		session = sessionFactory.getCurrentSession();
		Season season = (Season) session.createQuery(
				"from Season where seasonName='" + name + "'").uniqueResult();
		return season;
	}

	
	public Season findSeasonById(int id) {
		session = sessionFactory.getCurrentSession();
		Season result = (Season) session.createQuery(
				"from Season s where s.id=" + id).uniqueResult();
		return result;
	}
}
